<?php
namespace Admin\Controller;

class FeedbackController extends \Admin\Controller\AdminController
{
	/**
	 * 查询返馈信息
	 * @author 刘新
	 */
	public function feedbackList()
	{
		$where['status'] = array('egt', 0);
	
		//查询条件
		if(I('condition')) {
			$con = I('condition');
			if(preg_match('/^[1-9]\d*$/', $con)) {
				$where['id'] = $con;
			} else if(preg_match('/^[1-9]\d{10}$/', $con) ){
				$where['mobile'] = $con;
			}
		}
	
		$list = $this->lists('Feedback', $where, 'status desc,create_time desc');
		int_to_string($list, array('status' => C('FEEDBACK')));
	
		$this->assign('_title', '用户返馈信息列表');
		$this->assign('_list', $list);
		$this->display('list');
	}
	
	/**
	 * 编辑基金产品
	 * @author 刘新
	 */
	public function editFeedback()
	{
		$id = I('id', '', '/^\d+$/');
		!$id && $this->error('请选择要处理的用户反馈信息');
	
		if(IS_GET) {
			//查询反馈信息
			$feedback = $this->returnOne('Feedback', array('id' => $id, 'status' => array('gt', 0)));
			!$feedback && $this->error('查询失败');
			
			//查询反馈信息处理日志
			$log = $this->feedbackLog(array('feedback_id' => $id), 'create_time desc');
			//$log = $this->returnList('FeedbackLog', array('feedback_id' => $id), 'create_time desc');
			//if(!$log) $log = array();
			
			$feedback = array($feedback);
			int_to_string($feedback, array('status' => C('FEEDBACK')));

			$this->assign('_title', '处理用户反馈信息');
			$this->assign('log', $log);
			$this->assign('feedback', $feedback[0]);
			$this->display('edit');
		}
	
		//编辑
		$note = I('content', '');
		if(!$note) $this->error('请传入备注信息');
		$status = I('status', '', '/^\d$/');
		if($status !== '0' && !$status) $this->error('请选择处理状态');
		
		//反馈信息数据
		$data['status'] = $status;
		$data['update_time'] = NOW_TIME;
		$data['id'] = $id;
		
		//日志数据
		$log['feedback_id'] = $id;
		$log['content'] = $note;
		$log['admin_id'] = session('user_auth')['uid'];
		$log['admin_name'] = session('user_auth')['username'];
		$log['create_time'] = NOW_TIME;
		$log['update_time'] = NOW_TIME;
		$log['status'] = 1;
		$log['feedback_status'] = $status;
		
		$model = M();
		
		try {
			//更新反馈信息
			$res = $model->table('__FEEDBACK__')->save($data);
			if(!$res) throw new \Think\Exception('反馈信息更新失败');
			
			//记录日志
			$res = $model->table('__FEEDBACK_LOG__')->add($log);
			if(!$res) throw new \Think\Exception('日志更新失败');
			
			$info = true;
			$model->commit();
		} catch (\Exception $e) {
			$model->rollback();
			$info = $e->getMessage();
		}
		
		if($info === true) $this->success('编辑成功', U('Feedback/feedbackList'));
		$this->error($info);
	}
	
	/**
	 * 查询反馈信息详情
	 * @author 刘新
	 */
	public function feedbackDetail()
	{
		$id = I('id', '', '/^\d+$/');
		!$id && $this->error('参数错误');
		
		//查询反馈信息
		$feedback = $this->returnOne('Feedback', array('id' => $id, 'status' => array('egt', 0)));
		!$feedback && $this->error('查询失败');

		//查询反馈信息处理日志
		$log = $this->feedbackLog(array('feedback_id' => $id), 'create_time desc');
			
		$feedback = array($feedback);
		int_to_string($feedback, array('status' => C('FEEDBACK')));
		
		$this->assign('_title', '处理用户反馈信息');
		$this->assign('log', $log);
		$this->assign('feedback', $feedback[0]);
		$this->display('detail');
	}
	
	/**
	 * 查询日志
	 * ＠@author 刘新
	 */
	public function feedbackLog($where, $order='')
	{
		$log = $this->returnList('FeedbackLog', $where, $order);
		if(!$log) $log = array();
		int_to_string($log, array('feedback_status' => C('FEEDBACK')));
		return $log;		
	}
}